﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelSoft.HotelManagement_BUS;
using System.Collections;
using HotelSoft.HotelManagement_DTO;

namespace HotelSoft.HotelManagement_GUI
{
    public partial class frmBaocao_QUI : Form
    {
        // Define the CS_DROPSHADOW constant
        private const int CS_DROPSHADOW = 0x00020000;

        // Override the CreateParams property
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ClassStyle |= CS_DROPSHADOW;
                return cp;
            }
        }
        THONGKE_QUIDTO dtoTK;
        THONGKE_QUIBUS busTK = new THONGKE_QUIBUS();
        public frmBaocao_QUI()
        {
            InitializeComponent();
        }

        private void frmBaocao_QUI_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            lblTongsoDT.Text = "0";
            lblTongsoSLT.Text = "0";
            cboQui.SelectedIndex = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Double temp = 1;
            this.Opacity = this.Opacity + 0.1;
            if (this.Opacity == temp)
            {
                timer1.Enabled = false;
            }
        }


        public void LoadListViewMD(int iQui, int iNam)
        {
            dtoTK = new THONGKE_QUIDTO();
            dtoTK.QUI = iQui;
            dtoTK.NAM = iNam;
            lvTKMD.Items.Clear();
            ListViewItem item;
            DataTable tbl = new DataTable();
            tbl = busTK.KTMD_PHONG(dtoTK);
            DataTable tbl_T = new DataTable();
            tbl_T = busTK.KTMD_PHONG_Tong(dtoTK);
            float Sum = 0;
            if (tbl_T.Rows.Count != 0)
                Sum = float.Parse(tbl_T.Rows[0].ItemArray[0].ToString());
            int STT = 1;
            foreach(DataRow dr in tbl.Rows)
            {
                item = new ListViewItem();
                item.Text = STT.ToString();
                item.SubItems.Add(dr["SoPhong"].ToString());
                item.SubItems.Add(dr["Soluot"].ToString());
                float tile;
                tile = (int.Parse(dr["Soluot"].ToString()) * 100) / Sum;
                item.SubItems.Add(tile.ToString("#,#0")+"%");
                lvTKMD.Items.Add(item);
                STT++;
            }
            lblTongsoSLT.Text = Sum.ToString();
        }

        public void LoadListViewDT(int iQui, int iNam)
        {
            dtoTK = new THONGKE_QUIDTO();
            dtoTK.QUI = iQui;
            dtoTK.NAM = iNam;
            lvTKDT.Items.Clear();
            ListViewItem item;
            DataTable tbl = new DataTable();
            tbl = busTK.KTDT_LOAIPHONG(dtoTK);

            DataTable tbl_TT = new DataTable();
            tbl_TT = busTK.KTDT_LOAIPHONG_Tong(dtoTK);
            float Sum;
            if (tbl_TT.Rows[0].ItemArray[0].ToString() == "")
            {
                lblTongsoDT.Text = "0";
                return;
            }
            Sum = float.Parse(tbl_TT.Rows[0].ItemArray[0].ToString());
            int STT = 1;
            foreach (DataRow dr in tbl.Rows)
            {
                item = new ListViewItem();
                item.Text = STT.ToString();
                item.SubItems.Add(dr["TenLP"].ToString());
                item.SubItems.Add(Decimal.Parse(dr["TC"].ToString()).ToString("#,##0 VNĐ"));
                float tile;
                tile = (int.Parse(dr["TC"].ToString()) * 100) / Sum;
                item.SubItems.Add(tile.ToString("#,##0") + "%");
                lvTKDT.Items.Add(item);
                STT++;
            }
            lblTongsoDT.Text = Sum.ToString("#,##0 VNĐ");
        }

        private void Btn_ThongKe_Click(object sender, EventArgs e)
        {
            if (txtNam.Text.Trim() == "0")
            {
                MessageBox.Show("Xin hãy nhập năm!");
                txtNam.Focus();
                return;
            }
            if (cboQui.SelectedIndex != -1)
            {
                if (tabControl1.SelectedTabIndex == 0)
                {
                    lblQui_DT.Text = cboQui.SelectedItem.ToString();
                    lblNamDT.Text = txtNam.Text;
                    LoadListViewDT(int.Parse(cboQui.SelectedItem.ToString()),int.Parse(txtNam.Text));
                }
                else
                {
                    lblQui_MD.Text = cboQui.SelectedItem.ToString();
                    lblNamMD.Text = txtNam.Text;
                    LoadListViewMD(int.Parse(cboQui.SelectedItem.ToString()), int.Parse(txtNam.Text));
                }
            }
            else
            {
                lblQui_DT.Text = "...";
                lblQui_MD.Text = "...";
                MessageBox.Show("Xin hãy chọn Quí cần thống kê!");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtNam_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void txtNam_Validating(object sender, CancelEventArgs e)
        {
            if (txtNam.Text.Length == 0 || txtNam.Text.Trim() == "")
            {
                txtNam.Text = "0";
            }
            if(int.Parse(txtNam.Text)<1990 || int.Parse(txtNam.Text)>=9999)
            {
                MessageBox.Show("Hãy nhập lại năm (1990 -> 9999)");
                txtNam.Focus();
                return;
            }
        }
    }
}
